<!--
 * @Author: spycym
 * @Date: 2022-07-13 20:29:50
 * @LastEditTime: 2022-07-21 18:32:49
 * @LastEditors: spycym
 * @Description: 
 * 不要随便动我的代码哟!
-->
<template>
  <div id="app">
    <div class="todo-container">
      <div class="todo-wrap">
        <todoHeaderVue @addTodoObj="addTodoObj" />
        <todoListVue :todos="todos" />
        <todoFooterVue
          :todos="todos"
          @isAllDone="isAllDone"
          @clearAllTodo="clearAllTodo"
        />
      </div>
    </div>
  </div>
</template>

<script>
import todoHeaderVue from "./components/todoHeader.vue";
import todoListVue from "./components/todoList.vue";
import todoFooterVue from "./components/todoFooter.vue";
export default {
  name: "App",
  components: {
    todoFooterVue,
    todoHeaderVue,
    todoListVue,
  },
  data() {
    return {
      todos: JSON.parse(localStorage.getItem("todos")) || [],
    };
  },
  methods: {
    addTodoObj(todo) {
      this.todos.unshift(todo);
    },
    deleteTodoObj(id) {
      this.todos = this.todos.filter((todo) => {
        return todo.id != id;
      });
    },
    changeTodoDone(id) {
      this.todos.forEach((todo) => {
        if (todo.id === id) {
          todo.done = !todo.done;
        }
      });
    },
    isAllDone(done) {
      this.todos.forEach((todo) => {
        todo.done = done;
      });
    },
    clearAllTodo() {
      this.todos = this.todos.filter((todo) => {
        return !todo.done;
      });
    },
    UpdateTodoObj(id, value) {
      this.todos.forEach((todo) => {
        if (todo.id === id) {
          todo.thing = value;
        }
      });
    },
  },
  watch: {
    todos: {
      deep: true,
      handler(value) {
        localStorage.setItem("todos", JSON.stringify(value));
      },
    },
  },
  mounted() {
    this.$bus.$on("addTodoObj", this.addTodoObj);
    this.$bus.$on("deleteTodoObj", this.deleteTodoObj);
    this.$bus.$on("changeTodoDone", this.changeTodoDone);
    this.$bus.$on("UpdateTodoObj", this.UpdateTodoObj);
  },
  beforeDestroy() {
    this.$bus.$off("addTodoObj");
    this.$bus.$off("deleteTodoObj");
    this.$bus.$off("changeTodoDone");
    this.$bus.$off("UpdateTodoObj");
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}
.btn-edit {
  color: #fff;
  background-color: skyblue;
  border: 1px solid rgb(38, 142, 184);
  margin-right: 10px;
}
.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn-edit:hover {
  color: #fff;
  background-color: rgb(38, 142, 184);
}
.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
